#include <AutoLock.hh>
#include "GroupCore.hh"

GroupCore::GroupCore(Lock* Key, WorkerChain* AssignedGroupCore)
{
	State = false;
	
	LockOfWorkingCount = Key;
	checkNullFV(LockOfWorkingCount, "LockOfWorkingCount is NULL.");
	
	ListOfWorker = AssignedGroupCore;
	checkNullFV(ListOfWorker, "ListOfWorker is NULL.");
	
	WorkCount = 0;
	MinNumOfWorker = 0;
	MaxNumOfWorker = 0;
	NumOfWorkingWorker = 0;
	
	State = true;
}

ResultOf GroupCore::enter()
{
	if (!State)
	{
		fprintf(stderr, "This instance(%x) is not available.\n", (Number)this);
		return RETURN_PARAM_ERROR;
	}
	
	AutoLock AutoKey(LockOfWorkingCount);
	
	++WorkCount;
	++NumOfWorkingWorker;
	return RETURN_OK;
}

ResultOf GroupCore::exit()
{
	if (!State)
	{
		fprintf(stderr, "This instance(%x) is not available.\n", (Number)this);
		return RETURN_PARAM_ERROR;
	}
	
	AutoLock AutoKey(LockOfWorkingCount);
	
	--NumOfWorkingWorker;
	return RETURN_OK;
}

USNumber GroupCore::tellWorking()
{
	if (!State)
	{
		fprintf(stderr, "This instance(%x) is not available.\n", (Number)this);
		return 0;
	}
	
	return NumOfWorkingWorker;
}

USNumber GroupCore::tellAvailable()
{
	if (!State)
	{
		fprintf(stderr, "This instance(%x) is not available.\n", (Number)this);
		return 0;
	}
	
	return ListOfWorker->tellSize() - NumOfWorkingWorker;
}

USNumber GroupCore::tellNumOfCompletedTasks()
{
	if (!State)
	{
		fprintf(stderr, "This instance(%x) is not available.\n", (Number)this);
		return 0;
	}
	
	return WorkCount;
}

USNumber GroupCore::tellMin()
{
	if (!State)
	{
		fprintf(stderr, "This instance(%x) is not available.\n", (Number)this);
		return 0;
	}
	
	return MinNumOfWorker;
}

USNumber GroupCore::tellMax()
{
	if (!State)
	{
		fprintf(stderr, "This instance(%x) is not available.\n", (Number)this);
		return 0;
	}
	
	return MaxNumOfWorker;
}

ResultOf GroupCore::setMin(USNumber NumOfWorker)
{
	if (!State)
	{
		fprintf(stderr, "This instance(%x) is not available.\n", (Number)this);
		return RETURN_ERROR;
	}
	
	MinNumOfWorker = NumOfWorker;
	return RETURN_OK;
}

ResultOf GroupCore::setMax(USNumber NumOfWorker)
{
	if (!State)
	{
		fprintf(stderr, "This instance(%x) is not available.\n", (Number)this);
		return RETURN_ERROR;
	}
	
	MaxNumOfWorker = NumOfWorker;
	return RETURN_OK;
}
